package cn.com.kgo.entity.util;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;

/**
 * @author keepgoon
 * @Description: TODO
 * @date 2019/5/1715:47
 */
@Setter
@Getter
public class KResult implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer code;
    private String msg;
    private Long count;
    private Object data;

    private static final Integer NOMAL_CODE = 0;
    private static final Integer SERVER_ERROR_CODE = 500;
    // 是否成功字段
    private static String SUCCESS = "success";
    // 影响行数
    private static String EFFECT = "effect";
    // 提示信息
    private static String MSG = "msg";
    private static String CODE = "code";
    private static String COUNT = "count";
    private static String DATA = "data";

    // 新增失败时的提示
    private static String ADD_FAIL = "新增失败！";
    private static String UPDATE_FAIL = "更新失败！";
    private static String DELETE_FAIL = "删除失败！";

    /**
     * 新增处理方法
     * @param addRow
     * @return
     */
    public static String add(int addRow) {
        if (addRow > 0) {
            return succeedAdd(addRow);
        } else {
            return failAdd();
        }
    }
    public static String succeedAdd(int addRow) {
        JSONObject result_json = new JSONObject();
        // 新增成功
        result_json.put(SUCCESS, true);
        // 影响行数
        result_json.put(EFFECT, "新增成功，新增" + addRow + "行。");
        return result_json.toJSONString();
    }
    public static String failAdd() {
        JSONObject result_json = new JSONObject();
        // 新增成功
        result_json.put(SUCCESS, false);
        // 影响行数
        result_json.put(MSG, ADD_FAIL);
        return result_json.toJSONString();
    }
    public static String failAdd(String msg) {
        JSONObject result_json = new JSONObject();
        // 新增成功
        result_json.put(SUCCESS, false);
        // 影响行数
        result_json.put(MSG, msg);
        return result_json.toJSONString();
    }


    /**
     * 更新处理方法
     * @param updateRow
     * @return
     */
    public static String update(int updateRow) {
        if (updateRow > 0) {
            return succeedUpdate(updateRow);
        } else {
            return failUpdate();
        }
    }
    public static String succeedUpdate(int updateRow) {
        JSONObject result_json = new JSONObject();
        // 新增成功
        result_json.put(SUCCESS, true);
        // 影响行数
        result_json.put(EFFECT, updateRow);
        return result_json.toJSONString();
    }
    public static String failUpdate() {
        JSONObject result_json = new JSONObject();
        // 新增成功
        result_json.put(SUCCESS, false);
        // 影响行数
        result_json.put(MSG, UPDATE_FAIL);
        return result_json.toJSONString();
    }

    public static String delete(int updateRow) {
        if (updateRow > 0) {
            return succeedDelete(updateRow);
        } else {
            return failDelete();
        }
    }

    public static String succeedDelete(int addRow) {
        JSONObject result_json = new JSONObject();
        // 更新成功
        result_json.put(SUCCESS, true);
        // 影响行数
        result_json.put(EFFECT, "更新成功，更新" + addRow + "行。");
        return result_json.toJSONString();

    }

    public static String failDelete() {
        JSONObject result_json = new JSONObject();
        result_json.put(SUCCESS, false);
        result_json.put(MSG, DELETE_FAIL);
        return result_json.toJSONString();
    }




    public static <T> String queryFail(String msg) {
        JSONObject result_json = new JSONObject();
        // 新增成功
        result_json.put(CODE, 0);
        result_json.put(SUCCESS, false);
        // 影响行数
        result_json.put(MSG, msg);
        result_json.put(COUNT, 0);
        result_json.put(DATA, null);
        return result_json.toJSONString();
    }

    public static <T> String querySucceed(int count, List<T> data) {
        JSONObject result_json = new JSONObject();
        // 新增成功
        result_json.put(CODE, 0);
        // 影响行数
        result_json.put(MSG, "");
        result_json.put(SUCCESS, true);
        result_json.put(COUNT, count);
        result_json.put(DATA, data);
        return result_json.toJSONString();
    }

    public static <T> String querySucceed(int count, List<T> data, String msg) {
        JSONObject result_json = new JSONObject();
        // 新增成功
        result_json.put(CODE, 0);
        // 影响行数
        result_json.put(MSG, msg);
        result_json.put(COUNT, count);
        result_json.put(SUCCESS, true);
        result_json.put(DATA, data);
        return result_json.toJSONString();
    }

    public static String querySucceed(int count, Object data, String msg) {
        JSONObject result_json = new JSONObject();
        // 新增成功
        result_json.put(CODE, 0);
        // 影响行数
        result_json.put(MSG, msg);
        result_json.put(COUNT, count);
        result_json.put(SUCCESS, true);
        result_json.put(DATA, data);
        return result_json.toJSONString();
    }

}
